#!/bin/bash

# 获取最新版
# Url=$(curl -Ls https://github.com/prometheus/prometheus/releases |awk -F'[ "]+' '/download.*prometheus-.*.linux-amd64.tar.gz/{print "https://github.com"$4 }' |head -n 1)
# curl -Lso /tmp/prometheus.tar.gz $Url

# 下载软件
VERSION=2.12.0
curl -SLO http://dl.hc-yun.com/soft/monitor/prometheus-${VERSION}.linux-amd64.tar.gz

# 解压配置
INSTALL_DIR=/usr/local/prometheus
[ -d "$INSTALL_DIR" ] && mv $INSTALL_DIR ${INSTALL_DIR}-$(date +%s)
tar xf prometheus-${VERSION}.linux-amd64.tar.gz
mv prometheus-${VERSION}.linux-amd64 $INSTALL_DIR

# 创建服务
cat > /usr/lib/systemd/system/prometheus.service <<EOF
[Unit]
Description=prometheus server daemon
After=network.target

[Service]
Restart=on-failure
ExecStart=${INSTALL_DIR}/prometheus --config.file=${INSTALL_DIR}/prometheus.yml
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF

# 启动服务跟随系统启动
systemctl daemon-reload
systemctl enable prometheus
systemctl restart prometheus
systemctl status prometheus